package com.itheima;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

@WebServlet("/checktudentservlet")
public class checktudentservlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        读取文件中的数据
        BufferedReader br = new BufferedReader(new FileReader("E:\\e.txt"));
//        创建集合保存学生对象
        ArrayList<Student> list = new ArrayList<>();
        resp.setContentType("text/html;charset=UTF-8");
//     循坏读写
        String line;
        while ((line=br.readLine())!=null){
            String[] sp = line.split(",");
            Student stu = new Student(sp[0],Integer.parseInt(sp[1]),Integer.parseInt(sp[2]));
            list.add(stu);
        }
        //将集合存入会话域中
        req.getSession().setAttribute("studnets",list);
        //重定向到web页面
        resp.sendRedirect("/stu/checkstudent.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
